home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Code Resources / Jims CDEFs 1.50 / demo Source ƒ / demoShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-27  |  15.5 KB  |  650 lines  |  [TEXT/KAHL]

  1. // -----------------------------------------------------------------------------
  2. //    File        : demoShell.c
  3. //    Date        : October 14, 1994
  4. //    Author        : somebody, modified by Jim Stout
  5. //    Purpose        : A simple shell program to drive demos of "jimsCDEFs" in
  6. //                : both a normal window and a MovableModal dialog.
  7. //                :
  8. //                : see demoDialog.c for the demoCDEF Dialog and,
  9. //                :      demoWind.c for routines used for the demoCDEF Window.
  10. //
  11. // -----------------------------------------------------------------------------
  12.  
  13. #include <CommResources.h>            // Only needed for System 6 < MacII ROMs
  14. #include <CTBUtilities.h>            // ditto
  15.  
  16. #include <GestaltEqu.h>                // Toolbox 
  17. #include <Traps.h>
  18.  
  19. #include "demoShell.h"                // demo routines
  20. #include "demoDialog.h"
  21. #include "demoTab.h"
  22. #include "demoWind.h"
  23.  
  24. #include "dialogAssist.h"            // utility routines
  25. #include "movableModal.h"
  26.  
  27. #include "jimsCDEF.h"                // cdef stuff
  28.  
  29. #define GROWICON false
  30.  
  31. //=============================================================================
  32. // demoShell globals
  33. //=============================================================================
  34.  
  35. SysEnvRec        gSysEnv;
  36. Boolean            gQuit;
  37. Boolean            gInBackground;
  38.  
  39. short            gDemoType;    
  40. ControlHandle    gPopCtl, gPopCtl2, gPopCtl3, gBoxCtl,gRad1,gRad2;
  41. ControlHandle    gProgBar1, gProgBar2, gProgBar3;
  42.             
  43.  
  44. extern    pascal void CDmain();            // entry point to CDEF in cdef3D.c
  45. //=============================================================================
  46. // demo entry point
  47. //=============================================================================
  48.  
  49. main()
  50. {
  51.     MaxApplZone();
  52.     initToolbox();
  53.     getEvents();
  54.     return(0);
  55. }
  56.  
  57. //=============================================================================
  58. //    initialize Mac Toolbox & check for right environment
  59. //=============================================================================
  60. void initToolbox()
  61. {
  62.     Handle        menuBar;
  63.     EventRecord thisEvent;
  64.     short        count;
  65.  
  66.     gInBackground = FALSE;
  67.     gQuit = FALSE;
  68.  
  69.     InitGraf((Ptr) &qd.thePort);
  70.     InitFonts();
  71.     InitWindows();
  72.     InitMenus();
  73.     TEInit();
  74.     InitDialogs(NIL);
  75.     InitCursor();
  76.     
  77.     if(daGestalt(gestaltCTBVersion) >= 0x0100) {
  78.         InitCRM();                        // Only needed for System 6 < MacII ROMs
  79.         InitCTBUtilities();                // ditto
  80.     }
  81.         
  82.     for (count = 1; count <= 3; ++count)
  83.         EventAvail(everyEvent, &thisEvent);
  84.         
  85.     SysEnvirons(curSysEnvVers, &gSysEnv);
  86.  
  87.     if (gSysEnv.machineType < 0)
  88.         doAlert(errNeedMacPlus, true);
  89.  
  90.     if (gSysEnv.systemVersion < 0x0600)
  91.         doAlert(errNeedSys6, true);
  92.  
  93.     if (!trapIsAvailable(_WaitNextEvent))
  94.         doAlert(errNeedMultiF, true);
  95.  
  96.     menuBar = GetNewMBar(rMenuBar);
  97.     if ( menuBar == NIL )
  98.          doAlert(errBadRsrc, true);
  99.          
  100.     SetMenuBar(menuBar);
  101.     DisposHandle(menuBar);
  102.     
  103.     AddResMenu(GetMHandle(mApple), 'DRVR');    // Add DA names to Apple menu
  104.     
  105.     doNewWindow();                            // create demo Window
  106.     adjustMenus();
  107.     DrawMenuBar();
  108. }
  109.  
  110. //=============================================================================
  111. // our main event loop.  We drive our Progress bars from here.
  112. //=============================================================================
  113. void getEvents()
  114. {
  115.     RgnHandle    cursorRgn;
  116.     Boolean        gotEvent;
  117.     EventRecord    thisEvent;
  118.     long        sleep;
  119.     
  120.     if(gSysEnv.systemVersion < 0x0700)
  121.         sleep = 1L;                            // so progress bars work
  122.     else
  123.         sleep = MAXLONG;
  124.  
  125.     cursorRgn = NIL;
  126.     while ( !gQuit ) {
  127.         gotEvent = WaitNextEvent(everyEvent, &thisEvent, sleep, cursorRgn);
  128.         if ( gotEvent ) {
  129.             doProcessEvents(&thisEvent);
  130.         }
  131.         doPeriodicEvent(&thisEvent);
  132.     }
  133. }
  134.  
  135. //=============================================================================
  136. //    process our events
  137. //=============================================================================
  138. void doProcessEvents(EventRecord *thisEvent)
  139. {
  140.     switch ( thisEvent->what ) {
  141.         case mouseDown:
  142.             doMouseDown(thisEvent);
  143.         break;
  144.         
  145.         case keyDown:
  146.         case autoKey:
  147.             doKeyPress(thisEvent);
  148.         break;
  149.         
  150.         case activateEvt:
  151.             doActivate(thisEvent);
  152.             break;
  153.             
  154.         case updateEvt:
  155.             doUpdate(thisEvent);
  156.         break;
  157.             
  158.         case diskEvt:
  159.             diskEvent(thisEvent);            // in movableModal
  160.         break;
  161.             
  162.         case osEvt:
  163.             doOSEvent(thisEvent);
  164.         break;
  165.     }
  166. }
  167.  
  168. void doPeriodicEvent ( EventRecord * thisEvent )
  169. {
  170.  
  171. #pragma unused(thisEvent)
  172.  
  173.     if(isAppWindow(FrontWindow())) {
  174.         if(gDemoType == 6)
  175.             doProgress();
  176.     }
  177. }
  178.  
  179. //=============================================================================
  180. //    activate our window
  181. //=============================================================================
  182. void doActivate(EventRecord *thisEvent)
  183. {
  184.     WindowPtr    theWindow;
  185.     Boolean        becomingActive;
  186.  
  187.     theWindow = (WindowPtr) thisEvent->message;
  188.     becomingActive = (thisEvent->modifiers & activeFlag) != 0;
  189. #if GROWICON
  190.     if ( isAppWindow(theWindow) ) {
  191.         DrawGrowIcon(theWindow);
  192.     }
  193. #endif
  194. }
  195.  
  196. //=============================================================================
  197. //    doKeyPress
  198. //=============================================================================
  199. void doKeyPress(EventRecord *thisEvent)
  200. {
  201.     char    key;
  202.  
  203.     key = thisEvent->message & charCodeMask;
  204.     if ( thisEvent->modifiers & cmdKey ) {
  205.         adjustMenus();
  206.         doMenuCommand(MenuKey(key));
  207.     } else {
  208. //        doKeyPress(thisEvent);
  209.     }
  210. }
  211.  
  212. //=============================================================================
  213. //    react to a MouseDown in our window
  214. //=============================================================================
  215. void doMouseDown(EventRecord *thisEvent)
  216. {
  217.     long        newSize;
  218.     Rect        growRect;
  219.     WindowPtr    theWindow;
  220.     short        part = FindWindow(thisEvent->where, &theWindow);
  221.  
  222.     switch ( part ) {
  223.         case inMenuBar:
  224.             adjustMenus();
  225.             doMenuCommand(MenuSelect(thisEvent->where));
  226.         break;
  227.             
  228.         case inSysWindow:
  229.             SystemClick(thisEvent, theWindow);
  230.         break;
  231.         
  232.         case inContent:
  233.             if ( theWindow != FrontWindow() )
  234.                 SelectWindow(theWindow);
  235.             else
  236.                 doContentClick(thisEvent, theWindow);
  237.         break;
  238.         
  239.         case inDrag:                // Pass screenBits.bounds to get all gDevices
  240.             DragWindow(theWindow, thisEvent->where, &qd.screenBits.bounds);
  241.         break;
  242.         
  243.         case inGrow:
  244.             growRect = qd.screenBits.bounds;
  245.             growRect.top = growRect.left = 80;        // Arbitrary minimum size.
  246.             newSize = GrowWindow(theWindow, thisEvent->where, &growRect);
  247.             if (newSize != 0) {
  248.                 invalidateScrollbars(theWindow);
  249.                 SizeWindow(theWindow, LOWORD(newSize), HIWORD(newSize), TRUE);
  250.                 invalidateScrollbars(theWindow);
  251.             }
  252.         break;
  253.         
  254.         case inGoAway:
  255.             if (TrackGoAway(theWindow, thisEvent->where))  {
  256.                 closeAnyWindow(theWindow);
  257.             }
  258.         break;
  259.         
  260.         case inZoomIn:
  261.         case inZoomOut:
  262.             if (TrackBox(theWindow, thisEvent->where, part)) {
  263.                 SetPort(theWindow);
  264.                 EraseRect(&theWindow->portRect);
  265.                 InvalRect(&theWindow->portRect);
  266.             }
  267.         break;
  268.     }
  269. }
  270.  
  271. //=============================================================================
  272. //    doContent Click
  273. //=============================================================================
  274. void doContentClick ( EventRecord *thisEvent, WindowPtr theWindow)
  275. {
  276.     Point            mousePt;
  277.     short            thePart;
  278.     ControlHandle    theCtl;
  279.     
  280.     mousePt = thisEvent->where;
  281.     GlobalToLocal(&mousePt);
  282.     thePart = FindControl(mousePt, theWindow, &theCtl);
  283.     
  284.     if(thePart && (theCtl == gPopCtl || 
  285.                     theCtl == gPopCtl2 ||
  286.                      theCtl == gPopCtl3 ||
  287.                       gDemoType == 2)) {
  288.         TrackControl(theCtl, mousePt,  (ControlActionUPP)-1);
  289.         if(theCtl == gPopCtl)
  290.             doSwapControls(theWindow);
  291.         else
  292.         if(theCtl == gPopCtl2)
  293.             doChangeColor(theWindow);
  294.         else
  295.         if(theCtl == gPopCtl3)
  296.             doChangePat(theWindow, true);
  297.     }
  298.     else
  299.     if(thePart && theCtl) {
  300.         thePart = TrackControl(theCtl, mousePt, nil);
  301.         if(thePart)
  302.             doControlClick(theCtl, thePart);
  303.     }
  304.     
  305. }
  306. //=============================================================================
  307. //    doOSEvent
  308. //=============================================================================
  309. void doOSEvent(EventRecord *thisEvent)
  310. {
  311.     switch ((thisEvent->message >> 24) & 0x00FF) {        // High byte of message
  312.         case suspendResumeMessage:
  313.             gInBackground = (thisEvent->message & resumeFlag) == 0;
  314. #if GROWICON
  315.             if (FrontWindow()) {
  316.                 DrawGrowIcon(FrontWindow());
  317.             }
  318. #endif
  319.         break;
  320.         
  321.         case mouseMovedMessage:
  322.             break;
  323.     }
  324. }
  325.  
  326. //=============================================================================
  327. //    doUpdate
  328. //=============================================================================
  329. void doUpdate(EventRecord * thisEvent)
  330. {
  331.     WindowPtr theWindow = (WindowPtr)thisEvent->message;
  332.     
  333.     if ( isAppWindow(theWindow) ) {
  334.         BeginUpdate(theWindow);
  335.         if (!EmptyRgn(theWindow->visRgn)) {
  336.             SetPort(theWindow);
  337.             EraseRgn(theWindow->visRgn);
  338.             DrawControls(theWindow);
  339. #if GROWICON
  340.             DrawGrowIcon(theWindow);
  341. #endif
  342.         }
  343.         EndUpdate(theWindow);
  344.     }
  345. }
  346.  
  347. //=============================================================================
  348. //    adjustMenus
  349. //=============================================================================
  350. void adjustMenus()
  351. {
  352.     WindowPtr    window;
  353.     MenuHandle    menu;
  354.  
  355.     window = FrontWindow();
  356.  
  357.     menu = GetMHandle(mFile);
  358.     if ( window != NIL ) {
  359.         EnableItem(menu, iClose);
  360.     }
  361.     else {
  362.         DisableItem(menu, iClose);
  363.     }
  364.     menu = GetMHandle(mEdit);
  365.     if ( isDAWindow(window) ) {        // A desk accessory might need the edit menu…
  366.         EnableItem(menu, iUndo);
  367.         EnableItem(menu, iCut);
  368.         EnableItem(menu, iCopy);
  369.         EnableItem(menu, iClear);
  370.         EnableItem(menu, iPaste);
  371.     } else {                        // … but we don’t use it.
  372.         DisableItem(menu, iUndo);
  373.         DisableItem(menu, iCut);
  374.         DisableItem(menu, iCopy);
  375.         DisableItem(menu, iClear);
  376.         DisableItem(menu, iPaste);
  377.     }                
  378.     menu = GetMHandle(mDemo);
  379.     if ( window != NIL ) {
  380.         DisableItem(menu, iDemoWind);
  381.     }
  382.     else {
  383.         EnableItem(menu, iDemoWind);
  384.     }
  385. }
  386.  
  387. //=============================================================================
  388. //    disable or enable our menus
  389. //=============================================================================
  390. void disableMenus(Boolean disable)
  391. {
  392.     if(disable) {
  393.         DisableItem(GetMHandle(mFile), 0);
  394.         DisableItem(GetMHandle(mDemo), 0);
  395.     }
  396.     else {
  397.         EnableItem(GetMHandle(mFile), 0);
  398.         EnableItem(GetMHandle(mDemo), 0);
  399.     }
  400.     DrawMenuBar();
  401. }
  402. //=============================================================================
  403. //    doMenuCommand
  404. //=============================================================================
  405. void doMenuCommand(long menuResult)
  406. {
  407.     short        menuID;                // The resource ID of the selected menu
  408.     short        menuItem;            // The item number of the selected menu
  409.     Str255        daName;
  410.     
  411.     menuID = HIWORD(menuResult);
  412.     menuItem = LOWORD(menuResult);
  413.     switch ( menuID ) {
  414.         case mApple:
  415.             switch ( menuItem ) {
  416.                 case iAbout:
  417.                     (void) Alert(rAboutAlert, NIL);
  418.                 break;
  419.                 default:            // All non-About items in this menu are DAs
  420.                     GetItem(GetMHandle(mApple), menuItem, daName);
  421.                     (void) OpenDeskAcc(daName);
  422.                 break;
  423.             }
  424.         break;
  425.         case mFile:
  426.             switch ( menuItem ) {
  427.                 case iClose:
  428.                     closeAnyWindow(FrontWindow());
  429.                 break;
  430.                 case iQuit:
  431.                     gQuit = TRUE;
  432.                 break;
  433.             }
  434.         break;
  435.         case mEdit:
  436.             switch (menuItem) {
  437.                 // Call SystemEdit for DA editing & MultiFinder
  438.                 // since we don’t do any Editing
  439.                 case iUndo:
  440.                 case iCut:
  441.                 case iCopy:
  442.                 case iPaste:
  443.                 case iClear:
  444.                     (void) SystemEdit(menuItem-1);
  445.                 break;
  446.             }
  447.         break;
  448.         case mDemo:
  449.             switch (menuItem) {
  450.                 case iDemoWind:
  451.                     doNewWindow();                
  452.                 break;
  453.                 case iDemoDlog:
  454.                     HiliteMenu(0);
  455.                     disableMenus(true);        // shut off our menus
  456.                     
  457.                     demoDialog();
  458.                     
  459.                     disableMenus(false);    // bring our menus back
  460.                 break;
  461.                 case iComparePopup:
  462.                     HiliteMenu(0);
  463.                     disableMenus(true);        // shut off our menus
  464.                     
  465.                     comparePopup();
  466.                     
  467.                     disableMenus(false);    // bring our menus back
  468.                 break;
  469.                 case iDemoTab1:
  470.                 case iDemoTab2:
  471.                 case iDemoTab3:
  472.                 case iDemoTab4:
  473.                     HiliteMenu(0);
  474.                     disableMenus(true);        // shut off our menus
  475.                     
  476.                     demoTab(menuItem);
  477.                     
  478.                     disableMenus(false);    // bring our menus back
  479.                 break;
  480.                 case iDynMenu:
  481.                     HiliteMenu(0);
  482.                     disableMenus(true);        // shut off our menus
  483.                     
  484.                     dynMenu();
  485.                     
  486.                     disableMenus(false);    // bring our menus back
  487.                 break;
  488.                 case iGroupBox:
  489.                     HiliteMenu(0);
  490.                     disableMenus(true);        // shut off our menus
  491.                     
  492.                     demoBoxes();
  493.                     
  494.                     disableMenus(false);    // bring our menus back
  495.                 break;
  496.                 case iColorButtons:
  497.                     HiliteMenu(0);
  498.                     disableMenus(true);        // shut off our menus
  499.                     
  500.                     demoButtons();
  501.                     
  502.                     disableMenus(false);    // bring our menus back
  503.                 break;
  504.             }
  505.         break;
  506.     }
  507.     HiliteMenu(0);        // Unhighlight what MenuSelect or MenuKey hilited
  508. }
  509.  
  510. //=============================================================================
  511. //    close a window, regardless of type
  512. //=============================================================================
  513. void closeAnyWindow(WindowPtr window)
  514. {
  515.     if (isDAWindow(window)) {
  516.         CloseDeskAcc( ( (WindowPeek) window )->windowKind );
  517.     } 
  518.     else if (isDialogWindow(window)) {
  519.         HideWindow(window);
  520.     } 
  521.     else if (isAppWindow(window)) {            // has to be our window
  522.         doCloseWindow(window);                                        
  523.     }
  524. }
  525.  
  526. //=============================================================================
  527.  
  528. //    doAlert
  529.  
  530. //=============================================================================
  531. void doAlert(short errNumber, Boolean exit)
  532. {
  533.     short        itemHit;
  534.     Str255        theMessage;
  535.  
  536.     SetCursor(&qd.arrow);
  537.     GetIndString(theMessage, rErrorStrings, errNumber);
  538.     ParamText(theMessage, NIL, NIL, NIL);
  539.     itemHit = StopAlert(rErrorAlert, NIL);
  540.     if(exit)
  541.         ExitToShell();
  542. }
  543.  
  544. //=============================================================================
  545.  
  546. //    isAppWindow
  547.  
  548. //=============================================================================
  549. Boolean isAppWindow(WindowPtr window)
  550. {
  551.     short        windowKind;
  552.  
  553.     if ( window == NIL )
  554.         return false;
  555.     else {
  556.         windowKind = ((WindowPeek) window)->windowKind;
  557.         return ((windowKind >= userKind) || (windowKind == dialogKind));
  558.     }
  559. }
  560.  
  561. //=============================================================================
  562.  
  563. //    isDAWindow
  564.  
  565. //=============================================================================
  566. Boolean isDAWindow(WindowPtr window)
  567. {
  568.     if ( window == NIL )
  569.         return false;
  570.     else
  571.         return ( ((WindowPeek) window)->windowKind < 0 );
  572. }
  573.  
  574. //=============================================================================
  575.  
  576. //    isDialogWindow
  577.     
  578. //=============================================================================
  579. Boolean isDialogWindow(WindowPtr window)
  580. {
  581.     if ( window == NIL )
  582.         return false;
  583.     else
  584.         return ( ((WindowPeek) window)->windowKind == dialogKind );
  585. }
  586.  
  587. //=============================================================================
  588.  
  589. //    invalidateScrollbars
  590.  
  591. //=============================================================================
  592. void invalidateScrollbars(WindowPtr theWindow)
  593. {
  594.     Rect    tempRect;
  595.  
  596.     SetPort(theWindow);
  597.  
  598.     tempRect = theWindow->portRect;
  599.     tempRect.left = tempRect.right - 15;
  600.     InvalRect(&tempRect);
  601.     EraseRect(&tempRect);
  602.  
  603.     tempRect = theWindow->portRect;
  604.     tempRect.top = tempRect.bottom - 15;
  605.     InvalRect(&tempRect);
  606.     EraseRect(&tempRect);
  607. }
  608.  
  609. //=============================================================================
  610.  
  611. //    doNewWindow
  612.  
  613. //=============================================================================
  614. WindowPtr    doNewWindow()
  615. {
  616.     WindowPtr    theWind;
  617.  
  618.     if(gSysEnv.hasColorQD)
  619.         theWind = GetNewCWindow(kNewWindowID, NIL, (WindowPtr) -1);
  620.     else
  621.         theWind = GetNewWindow(kNewWindowID, NIL, (WindowPtr) -1);
  622.         
  623.     if (theWind != NIL) {
  624.         SetPort(theWind);
  625.         TextFont(geneva);
  626.         TextSize(9);
  627.         
  628. // Create some controls in the new window
  629.  
  630.         doNewControls(theWind);    
  631.         make3DControls(theWind);
  632.         gDemoType = 1;
  633.     
  634.         ShowWindow(theWind);
  635.         
  636.         return(theWind);
  637.     }
  638.     return(0L);
  639. }
  640.  
  641.  
  642.  
  643. //=============================================================================
  644. //    Close our window, since from a resource, use DisposeWindow
  645. //=============================================================================
  646. void    doCloseWindow(WindowPtr theWind)
  647. {
  648.     DisposeWindow(theWind);
  649.     gDemoType = 0;
  650. }